/* 	File Name: mission_coding_finish.do
	Data: missions_for_stata.dta
	Author: Mark Richardson
	Purpose: Finish coding missions
	Output: coded missions for merging with sfgs
	Date: 05/09/2016
*/

/*
*Note: Variables names refer to budget functions the GAO Report, "Federal Budget Obligations by Budget Fuction and Object Classification for Fiscal Year 2003 (GAO-04-834).
The Report is available at http://www.gao.gov/products/GAO-04-834. For example, m550 is the proporation of the agency's budget in the function Health (550) as
identified in the supplemental materrials of report.
*/

use C:\Users\richar33\Dropbox\Papers\expertise\budget_missions\missions_for_stata.dta, clear

recode m800 .=1 if agency=="General Services Administration" //GSA spreadsheet lists bureaus, not "General Services Administration", so code manually.

egen z=rowmax(m*)

replace z=round(z,0.0001)

count if z<.5
count if z<.6
count if z<.7
count if z<.8

list agency m* if z<.8 //okay, except cms and opm

tab agency if m570!=. //only CMS

*create a mission variable

foreach i of varl m*{ //need to round numbers so stata does not round them and cause an error in the loop below
replace `i'=round(`i',0.0001)
}

gen mission=.

foreach i of numl 050 150 250 270 300 350 370 400 450 500 550 570 600 650 700 750 800 900{
replace mission=`i' if float(m`i')==float(z) & z!=.
}

replace mission=050 if float(m050)==float(z) & z!=. //stata omits leading zero

tab mission

sort mission agency dept

*get list coded by data
list agency mission dept

stop

**************************************
*Code each department 
**************************************

*Don't code "Other"

*Code Office of Secretary based on dominant mission among agencies or proportion of total budget if not clear from agency missions

*USDA
list agency mission if dept=="Department of Agriculture"
*Note: NIFA was created in 2008; would probably be in subfuction 352 Agriculture research and services
foreach i in "National Institute of Food and Agriculture" "Office of the Secretary of Agriculture"{
replace mission=350 if agency=="`i'"
}

*DOC
list agency mission if dept=="Department of Commerce"
foreach i in "Bureau of Economic Analysis" "Office of the Secretary of Commerce"{
replace mission=370 if agency=="`i'"
}

*DOD
list agency mission if dept=="Department of Defense"
replace mission=050 if dept=="Department of Defense" & agency!="Other (DOD)"
*DHA describes itself as an integrated combat support agency, and 050 definition includes maintaining the armed forces
replace mission=050 if agency=="Defense Health Agency" //Only health category in DOD budget is retiree health;

*ED
list agency mission if dept=="Department of Education"
foreach i in "Office of the Secretary of Education"{
replace mission=500 if agency=="`i'"
}

*DOE
list agency mission if dept=="Department of Energy"
replace mission=270 if dept=="Department of Energy" & mission==. & agency!="Other (DOE)"

*HHS
list agency mission if dept=="Department of Health and Human Services"
foreach i in "Agency for Toxic Substances and Disease Registry" "Office of the Secretary of Health and Human Services"{
replace mission=550 if agency=="`i'"
}

*DHS
list agency mission if dept=="Department of Homeland Security"
*Note: Code agencies as federal law enforcment which is under administration of justice, based on budget of USCIS and Secret Service
foreach i in "Office of the Secretary of Homeland Security" "Transportation Security Administration" ///
"United States Customs and Border Protection" "United States Immigration and Customs Enforcement"{
replace mission=750 if agency=="`i'"
}

*HUD
list agency mission if dept=="Department of Housing and Urban Development"
*A category called Housing Programs has 78% of the for that cateogry budget in 371 Mortgage Credit, which is congruent with FHA 
*providing mortgage insurance and loans;
*70% of HUD's budget is housing assistance, with most of that in the Office of Public and Indian Housing
*15% is community development, so code office of the secretary as housing assistance, which is under income security
*code Other (HUD) as missing because of mixed mission - subtracting 371 Mortgage Credit due to GNMA and Housing Programs and Housing Assitance 604 due to
*Public and Indian Housing from total budget leave 51% of HUD's budget in Community Development 451 (also subtracts 30 of community development funds
*in Housing Programs from total funds in Community Development

foreach i in "Federal Housing Administration" "Office of Housing" "Office of Housing/Federal Housing Administration"{
replace mission=370 if agency=="`i'"
}

replace mission=600 if agency=="Office of the Secretary of Housing and Urban Development"

*DOI
list agency mission if dept=="Department of the Interior"
*Note: MMS which preceded BOEM and BSEE was coded 300
foreach i in "Bureau of Ocean Energy Management" "Bureau of Safety and Environmental Enforcement" "Office of the Secretary of the Interior"{
replace mission=300 if agency=="`i'"
}

*DOJ
list agency mission if dept=="Department of Justice"
foreach i in "Executive Office for United States Attorneys" "Office of the Attorney General" "Office of the United States Attorney" "Office of Legal Counsel"{
replace mission=750 if agency=="`i'"
}

list agency mission if dept=="Department of Labor"
*Note: BILA mainly works on labor standards, so code 600 for labor not for international affairs
*Note: Employment Standards Administration was broken into its four major program components - Office of Federal Contract Compliance Programs, 
*Office of Labor Management Standards, Office of Workers Compensation Programs and the Wage and Hour Division - on 11/8/2009.
*Employment Standards Aministration is in DOL data borken down as follows: 8% in 500, 83% in 600, 8% in 050 (atomic energy defense for some reasons)
*Office of Federal Contract Complinace Programs - enforces employment standards for government contractors and subcontractors - Other labor services (505)
*Office of Workers Compensation Programs  - administers four major disability compensation programs which provide wage replacement benefits, 
*medical treatment, vocational rehabilitation and other benefits to certain workers or their dependents who experience work-related injury or 
*occupational disease - 601 General retirement and disability insurance (excluding Social Security)
*Office of Labor Management Standards - Manages LMRDA to promote union democracy and financial integrity in private sector labor unions - 505 Other labor services
*Wage and Hour Division - Enforces the Fair Labor Standards Act and law applying to Federal contracts - 505
*Not clear why a large portion of Employment Standards Aministration budget is in Federal employee retirement and disability
foreach i in "Bureau of International Labor Affairs" "Office of Federal Contract Compliance Programs" "Office of Labor-Management Standards" ///
"Office of the Secretary of Labor" "Wage and Hour Division" "Women's Bureau"{
replace mission=500 if agency=="`i'"
}

replace mission=700 if agency=="Veterans' Employment and Training Service" //Code as Veterans Benefits and Services

*TREAS
list agency mission if dept=="Department of the Treasury"
*Bureau of the Fiscal Services created by merging the Bureau of the Public Debt and the Financial Management Service
*BPD is 800; FMS is 50% 800 & 45% 900 - which is interst payments; Code 800


foreach i in "Bureau of the Fiscal Service" "Office of the Secretary of the Treasury"{
replace mission=800 if agency=="`i'"
}

*VA
list agency mission if dept=="Department of Veterans Affairs"
replace mission=700 if dept=="Department of Veterans Affairs" & agency!="Other (VA)" //Code as Veterans Benefits and Services

*DOS
*94% of State's budget is in 150 International Affairs; Another 4% is in Federal employee retirement and disability; Only 2% in other functions
*Code all of State as 150
list agency mission if dept=="Department of State"
replace mission=150 if dept=="Department of State" & agency!="Other (DOS)"

*DOT
list agency mission if dept=="Department of Transportation"
*Note: All DOT budget functions are in a transportation subfunction other than a small part of the Maritime Administration's budget in defense
replace mission=400 if dept=="Department of Transportation" & mission ==. & agency!="Other (DOT)"

*Independent agencies
list agency if mission==. & dept==""

replace mission=800 if agency=="Administrative Conference of the United States" //mission is improve intra-executive branch administrative process
replace mission=370 if agency=="Board of Governors of the Federal Reserve System" //Same category as SEC, FTC, OCC, and NCUA
replace mission=370 if agency=="Consumer Financial Protection Bureau" //Same as above

replace mission=370 if agency=="Council of Economic Advisers" //Economic policy
replace mission=300 if agency=="Council on Environmental Quality" //Natural Resources and Environment
replace mission=370 if agency=="Federal Deposit Insurance Corporation" //Deposit insurance
replace mission=370 if agency=="Federal Home Loan Mortgage Corporation" //Mortgage credit
replace mission=370 if agency=="Federal Housing Finance Agency" //Mortgage credit
replace mission=370 if agency=="Federal National Mortgage Association" //Mortgage credit
replace mission=150 if agency=="Millennium Challenge Corporation" //International affairs
replace mission=500 if agency=="National Foundation on the Arts and the Humanities" //Same as NEH, NEA, IMS
replace mission=400 if agency=="National Railroad Passenger Corporation (AMTRAK)" //Transportation
replace mission=050 if agency=="National Security Staff" //National defense
replace mission=800 if agency=="Office of Management and Budget" //General government
replace mission=750 if agency=="Office of National Drug Control Policy" //Drug enforcement policy - High Intensity Drug Trafficking Program, also some public health
replace mission=250 if agency=="Office of Science and Technology Policy" //Per OSTP website provides "...advice on the scientific, engineering, and technological aspects of the economy, national security, homeland security, health, foreign relations, the environment, and the technological recovery and use of resources, among other topics.  OSTP also leads interagency science and technology policy coordination efforts, assists the Office of Management and Budget with an annual review and analysis of Federal research and development in budgets, and serves as a source of scientific and technological analysis and judgment for the President with respect to major policies, plans, and programs of the Federal Government.
replace mission=050 if agency=="Office of the Director of National Intelligence" //National defense
replace mission=370 if agency=="Office of the United States Trade Representative" //Based on coding of International Trade Administration
replace mission=370 if agency=="Postal Regulatory Commission" //Same as the Post Office
replace mission=050 if agency=="Selective Service System" //National defense - listed under DOD
replace mission=650 if agency=="Social Security Advisory Board"
replace mission=800 if agency=="United States Election Assistance Commission" //Election administration is best categorized as general government

foreach i of numl 050 150 250 270 300 350 370 400 450 500 550 570 600 650 700 750 800 900{
di `i'
list agency dept if mission==`i'
}


*recodes - to prevent narrow categories of Social Security and Medicare from creating indicators for agencies
replace mission=600 if agency=="Social Security Administration"|agency=="Social Security Advisory Board" //Recode to general income security
replace mission=550 if agency=="Centers for Medicare and Medicaid Services"

*recodes to put agencies in categories I believe are appropriate

*IRS is coded as Other income security (609) due to large dollar amount there, presumable due to tax refunds or tax credits;
*but mission is to collect taxes and enforce related law;
*Recoded IRS to 803 central fiscal operations, which is second largest budget category and more congruent with mission
replace mission=800 if agency=="Internal Revenue Service"

*OPM - large portion of budget is categorized as Federal Employee Retirement, which is coded as income security, but more appropriate coding is
*805 is central personnel management
replace mission=800 if agency=="Office of Personnel Management"

*label the mission variable
label define msn 50 "National Defense" 150 "International Affairs" 250 "General Science, Space, and Technology" 270 "Energy"
label define msn 300 "Natural Resources and Environment" 350 "Agriculture" 370 "Commerce and Housing Credit", add
label define msn 400 "Transportation" 450 "Community and Regional Development" 500 "Education, Training, Employment, and Social Services", add
label define msn 550 "Health" 570 "Medicare" 600 "Income Security" 650 "Social Security" 700 "Veterans Benefits and Services", add
label define msn 750 "Administration of Justice" 800 "General Government", add

label values mission msn

tab mission

save C:/Users/richar33/Dropbox/Papers/expertise/Data/mission.dta, replace

*create table for the appendix
decode mission, gen(m_text)

drop if mission==.

sort m_text agency dept

keep agency dept m_text

export excel using C:/Users/richar33/Dropbox/Papers/expertise/Data/mission_table.xlsx, first(var) replace

